// @ts-nocheck
import { onMounted, onBeforeUnmount, watch, ref } from 'vue';
import { publicUrls } from '@/utils/minxin';
import { shearConnfig } from '@/api/shear/shear';
import { useRoute } from 'vue-router';
const wx = ref(null);
const ShearTitle = ref('记忆网');
const ShearImage = ref('');
const route = useRoute();

const getimgs = async (imgs) => {
  var image = new Image();
  image.src = imgs;
  var base64;
  image.onload = async () => {
    base64 = await getBase64Url(image);
    ShearImage.value = base64;
    // console.log(base64);
  };
  return base64;
};
const getBase64Url = (img) => {
  var canvas = document.createElement('canvas');
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0, img.width, img.height);
  var ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase();
  var dataURL = canvas.toDataURL('image/' + ext);

  return dataURL;
  // ?imageView2/2/w/120/h/120/format/webp
};
export const shearInit = async (title: string, cover?: string, description?: string) => {
  // const script = document.createElement('script');
  // script.src = 'http://res.wx.qq.com/open/js/jweixin-1.6.0.js';
  // document.body.appendChild(script);
  // await new Promise((resolve) => {
  //   script.onload = resolve;
  // });
  //let urls = getimgs(cover ? cover + '?imageView2/2/w/40/h/40/format/webp' : publicUrls + 'door/shearLogo.jpg');
  // console.log(urls);
  ShearTitle.value = title;
  ShearImage.value = cover ? cover + '' : publicUrls + 'door/shearLogo.jpg';
  // ?imageView2/3/w/120/h/80/format/webp

  // publicUrls + 'door/logoRed.svg';
  const script = document.createElement('script');
  script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
  document.body.appendChild(script);
  script.onload = async () => {
    wx.value = window.wx; // window.jweixin;
    // initWeixinJSBridge();
    // wx.value = window.wx;
    let arr = await shearConnfig(window.location.href);
    console.log(arr);
    wx.value.config({
      // beta: true,
      debug: false,
      appId: 'wx3ac9a47605eadee8',
      timestamp: arr.data.jsTimestamp,
      nonceStr: arr.data.noncestr,
      signature: arr.data.signature,
      jsApiList: [
        'updateAppMessageShareData',
        'updateTimelineShareData',
        'onMenuShareAppMessage',
        'onMenuShareTimeline',
        'onMenuShareQQ',
        'onMenuShareQZone',
      ],
    });
    wx.value.ready(function () {
      //需在用户可能点击分享按钮前就先调用
      wx.value.updateAppMessageShareData({
        desc: description ? description : '记忆网-永不落幕的展览展示与交流平台', // 分享描述
        title: ShearTitle.value, // 分享标题
        link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: ShearImage.value, // 分享图标

        success: function () {
          // 设置成功
          console.log('设置成功', '分享给朋友记忆网-永不落幕的展览展示与交流平台');
        },
        cancel: function () {
          // 设置成功
        },
      });
      wx.value.updateTimelineShareData({
        title: ShearTitle.value, // 分享标题
        desc: description ? description : '记忆网-永不落幕的展览展示与交流平台', // 分享描述
        link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: ShearImage.value, // 分享图标
        success: function () {
          // 设置成功
          console.log('设置成功', '分享到微信朋友圈');
        },
      });
      if (!wx.value.updateAppMessageShareData) {
        wx.value.onMenuShareAppMessage({
          title: ShearTitle.value, // 分享标题
          desc: description ? description : '记忆网-永不落幕的展览展示与交流平台', // 分享描述
          link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: ShearImage.value, // 分享图标
          success: function () {
            console.log('设置成功', '分享给朋友记忆网-永不落幕的展览展示与交流平台');
            // 用户点击了分享后执行的回调函数
          },
        });
      }
    });
  };

  // 配置微信 JS-SDK
  // const appId = 'YOUR_APP_ID';
  // const timestamp = 'YOUR_TIMESTAMP';
  // const nonceStr = 'YOUR_NONCE_STR';
  // const signature = 'YOUR_SIGNATURE';
};
export const WXshareFirend = async (title: string, cover?: string) => {
  //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容\\

  ShearTitle.value = title;
  ShearImage.value = cover ? cover : publicUrls + 'door/logoRed.svg';
  //publicUrls + 'door/logoRed.svg';
  wx.value?.ready(function () {
    //需在用户可能点击分享按钮前就先调用
    wx.value.updateAppMessageShareData({
      desc: '记忆网-永不落幕的展览展示与交流平台', // 分享描述
      title: ShearTitle.value, // 分享标题
      link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: ShearImage.value, // 分享图标
      success: function () {
        // 设置成功
      },
      cancel: function () {
        // 设置成功
      },
    });
    wx.value.updateTimelineShareData({
      desc: '记忆网-永不落幕的展览展示与交流平台', // 分享描述
      title: ShearTitle.value, // 分享标题
      link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: ShearImage.value, // 分享图标
      success: function () {
        // 设置成功
      },
    });
    wx.value.onMenuShareAppMessage({
      title: ShearTitle.value, // 分享标题
      desc: '记忆网-永不落幕的展览展示与交流平台', // 分享描述
      link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: ShearImage.value, // 分享图标
      success: function () {
        // 用户点击了分享后执行的回调函数
      },
    });
  });
};
// 引入微信 JS-SDK
